<?
/*
*
* Class has simple interface to download any file from a server
* without displaying the location of the file
*
// Example:

require("Downloader.php");

$downloadfile = new Downloader("/dir1/dir2/dir3/filename.ext");
if (!$downloadfile->download()) echo "Sorry, we are experiencing technical difficulties downloading this file.";
*/


class Downloader
{
    public $url = "";
    public $headers = "";
    public $contenttype = "";
    public $contentdisposition = "";
    public $filename = "";
    private $_fp;


    public function __construct($url, $filename = "", $contenttype = "application/octet-stream", $contentdisposition = "attachment")
    {
        $this->url = $url;
        $this->contenttype = $contenttype;
        $this->contentdisposition = $contentdisposition;
        $this->filename = ($filename)? $filename : basename($url);
    }

    // check is specified file exists?
    public function exists()
    {
        if(file_exists($this->url)) return true;
        return false;
    }

    //get the filesize.
	public function size()
    {
        return filesize($this->url);
    }

    // download file
    public function download()
    {
		if($this->exists())
        {
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header('Content-Description: File Transfer');
            header("Content-type: ".$this->contenttype);
            header("Content-Disposition: ".$this->contentdisposition."; filename=\"".$this->filename."\"");
            header("Content-Length: ".$this->size());
            $headers? header("$headers") : null;
			ob_clean();
    		flush();
			return readfile($this->url);
        }
        return false;
	}

}

?>